package irri.soils.ssnm;

import irri.soils.ssnm.R;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityS extends SSNM implements View.OnClickListener, LocationListener, TextWatcher{
	private TextView latitudeField;
	private TextView longitudeField;
	private LocationManager locationManager;
	private String provider;
	private Button option1;

	private EditText name;
	private EditText cell;
	private EditText notes;
	private Bundle b;
	
	private Button button1;
	private EditText email;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.s);
        setCustomTitle();
        b = this.getIntent().getExtras();
        latitudeField = (TextView) findViewById(R.id.lat);
        longitudeField = (TextView) findViewById(R.id.lon);
        // Get the location manager
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		// Define the criteria how to select the location provider -> use
		// default
		Criteria criteria = new Criteria();
		provider = locationManager.getBestProvider(criteria, false);
		Location location = locationManager.getLastKnownLocation(provider);
		option1 = (Button) findViewById(R.id.button1);
		option1.setOnClickListener(this);
		
		name = (EditText) findViewById(R.id.name);
		cell = (EditText) findViewById(R.id.cell);
		email = (EditText) findViewById(R.id.email);
		notes = (EditText) findViewById(R.id.notes);
		button1 = (Button) findViewById(R.id.button1);
		name.setHint(get("farmer"));
		notes.setHint(get("add"));
		
        
		// Initialize the location fields
		if (location != null) {
			System.out.println("Provider " + provider + " has been selected.");
			Double lat =  (location.getLatitude());
			Double lng =  (location.getLongitude());
			latitudeField.setText(String.valueOf(lat));
			longitudeField.setText(String.valueOf(lng));
		} else {
			latitudeField.setText("0");
			longitudeField.setText("0");
		}
	    name.addTextChangedListener(this);
	    cell.addTextChangedListener(this);
	    
	    if(b.containsKey("name")){
	    	name.setText(b.getString("name"));
	    	cell.setText(b.getString("cell"));
	    	email.setText(b.getString("email"));
	    	notes.setText(b.getString("notes"));
	    	button1.setEnabled(true);
        }
    }

	@Override
	public void onClick(View v) {
		if(cell.getText().toString().length()==11){
		Intent intent = new Intent(ActivityS.this, ActivityQ.class);
		b.putDouble("lat", Double.parseDouble(""+latitudeField.getText()));
		b.putDouble("lon", Double.parseDouble(""+longitudeField.getText()));
		b.putString("name", ""+name.getText());
		b.putString("cell", ""+cell.getText());
		b.putString("email", ""+email.getText());
		b.putString("notes", ""+notes.getText());
		intent.putExtras(b);
		startActivity(intent);
		finish();
		}else{
			displayToast(get("invalid"));
		}
	}

	/* Request updates at startup */
	@Override
	protected void onResume() {
		super.onResume();
		locationManager.requestLocationUpdates(provider, 400, 1, this);
	}

	/* Remove the locationlistener updates when Activity is paused */
	@Override
	protected void onPause() {
		super.onPause();
		locationManager.removeUpdates(this);
		//finish();
	}

	@Override
	public void onLocationChanged(Location location) {
		Double lat = (location.getLatitude());
		Double lng = (location.getLongitude());
		latitudeField.setText(String.valueOf(lat));
		longitudeField.setText(String.valueOf(lng));
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(this, "Enabled new provider " + provider,
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(this, "Disenabled provider " + provider,
				Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public void onBackPressed() {
		Bundle b = this.getIntent().getExtras();
		Intent intent = new Intent(ActivityS.this, b.getInt("oorganic")==1 
				? ActivityY.class : ActivityX.class);
		intent.putExtras(b);
		startActivity(intent);
	}
   
	
    
    public void afterTextChanged(Editable s) {
    	String cellString = ""+cell.getText();	
    	if(!cellString.equals("")&&!cellString.startsWith("0")){
    		cell.setText("");
    		cell.append("0"+cellString);
    	}
    	appearNext();
    }
    
    public void appearNext(){
    	button1.setEnabled(!name.getText().toString().equals(""));
    }
    	
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    	
    }
        
    public void onTextChanged(CharSequence s, int start, int before, int count) {
            
    }
}